﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tic_tac_toe_sharp
{
    class TicTacToe
    {
        private char[,] board = new char[,] {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
        private int xcursor = 0;
        private int ycursor = 0;
        private int turn = 0;
        
        public TicTacToe()
        {          
        }
        private void DoTurn()
        {
            Console.SetCursorPosition(xcursor * 4, ycursor * 2);
            while (true)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);
                if (key.Key == ConsoleKey.UpArrow && ycursor > 0)
	            {
                        ycursor--;
	            }
                else if (key.Key == ConsoleKey.DownArrow && ycursor < 2)
                {
                        ycursor++;
                }
                else if (key.Key == ConsoleKey.LeftArrow && xcursor > 0)
                {
                        xcursor--;
                }
                else if (key.Key == ConsoleKey.RightArrow && xcursor < 2)
                {
                        xcursor++;
                }
                else if (key.Key == ConsoleKey.Enter && board[ycursor, xcursor] == ' ')
                {
                    if (turn == 0)
	                {
                        board[ycursor, xcursor] = 'x';
	                }
                    else
                    {
                        board[ycursor, xcursor] = 'o';  
                    }
                    turn = (turn + 1) % 2;
                    DrawBoard();
                    break;
                }
                Console.SetCursorPosition(xcursor * 4, ycursor * 2);
            }
        }
        private void DrawBoard()
        {
            Console.Clear();
            Console.WriteLine(board[0, 0] + " | " + board[0, 1] + " | " + board[0, 2]);
            Console.WriteLine("---------");
            Console.WriteLine(board[1, 0] + " | " + board[1, 1] + " | " + board[1, 2]);
            Console.WriteLine("---------");
            Console.WriteLine(board[2, 0] + " | " + board[2, 1] + " | " + board[2, 2]);
        }
        private void ClearBoard()
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    board[i, j] = ' ';
                }
            }
        }
        private char CheckBoard()
        {
            //Check vertical and horizontal
            for (int i = 0; i < 3; i++)
            {
                if (board[i, 0] != ' ' && board[i, 0] == board[i, 1] && board[i, 1] == board[i, 2])
                {
                    return board[i, 0];
                }
                else if (board[0, i] != ' ' && board[0, i] == board[1, i] && board[1, i] == board[2, i])
                {
                    return board[0, i];
                }
            }
            //Check diagonals
            if (board[0, 0] != ' ' && board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2])
            {
                return board[0, 0];
            }
            else if (board[2, 0] != ' ' && board[2, 0] == board[1, 1] && board[1, 1] == board[0, 2])
            {
                return board[2, 0];
            }
            //Check if free spaces left
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if(board[i, j] == ' ')
                    {
                        return 't';
                    }
                }
            }
            //If no more spaces and no winner
            return 'd';
        }
        public void Run()
        {
            while (true)
            {
                char c = 't';
                while (c == 't')
                {
                    DrawBoard();
                    DoTurn();
                    c = CheckBoard();
                }
                //End text
                Console.SetCursorPosition(10, 10);
                if (c != 'd')
                {
                    Console.WriteLine("Player " + c + " wins!"); 
                }
                else
                {
                    Console.WriteLine("Draw"); 
                }
                Console.SetCursorPosition(10, 12);
                Console.WriteLine("Press escape to exit or any key to play again..");
                if(Console.ReadKey(true).Key == ConsoleKey.Escape)
                {
                    break;
                }
                ClearBoard();
            }
        }

    }
}
